const unipay = require('uni-pay');
const createConfig = require('uni-config-center');

class Pay {
	unipayIns = null;
	version = 'v2';
	paytype = '';
	method = '';
	config = {};
	notifyUrl = '';
	constructor(paytype, method, notifyKey) {
		this.paytype = paytype;
		this.method = method;
		const config = createConfig({ pluginId: 'dx-epay' }).requireFile('config.js');
		if (!config || !config[paytype]) throw new Error('请先配置支付参数');
		this.config = config[paytype];
		const initConfig = {
			mchId: this.config.mchId,
		};
		if (paytype === 'wechat') {
			// 微信支付
			if (!this.config.enable) {
				throw new Error('请先配置微信支付参数');
			}
			this.version = this.config.version;
			// v2版本
			if (this.config.version === 'v2') {
				initConfig.key = this.config.key;
				initConfig.pfx = this.config.pfx;
			} else if (this.config.version === 'v3') {
				// v3版本
				initConfig.v3Key = this.config.v3Key;
				initConfig.appCertPath = this.config.appCertPath;
				initConfig.appPrivateKeyPath = this.config.appPrivateKeyPath;
			}
			switch (method) {
				case 'app':
					initConfig.appId = this.config.app.appId;
					break;
				case 'mp':
					initConfig.appId = this.config.mp.appId;
					break;
				case 'miniapp':
					initConfig.appId = this.config.miniapp.appId;
					break;
				case 'scan':
					initConfig.appId = this.config.native.appId;
					break;
				case 'wap':
					initConfig.appId = this.config.wap.appId;
					break;
			}
		} else if (paytype === 'alipay') {
			// 支付宝支付
			if (!this.config.enable) {
				throw new Error('请先配置支付宝支付参数');
			}
			initConfig.privateKey = this.config.privateKey;
			if (this.config.isCert) {
				initConfig.appCertPath = this.config.appCertPath;
				initConfig.alipayPublicCertPath = this.config.alipayPublicCertPath;
				initConfig.alipayRootCertPath = this.config.alipayRootCertPath;
			} else {
				initConfig.alipayPublicKey = this.config.alipayPublicKey;
			}
			switch (method) {
				case 'app':
					initConfig.appId = this.config.app.appId;
					break;
				case 'scan':
					initConfig.appId = this.config.native.appId;
					break;
				case 'miniapp':
					initConfig.appId = this.config.miniapp.appId;
					break;
			}
		}
		// 回调域名
		if(notifyKey && config.notifyUrl && config.notifyUrl[notifyKey] !== void 0){
			this.notifyUrl = config.notifyUrl[notifyKey];
		}
		// 初始化配置
		this.unipayIns = unipay.initWeixin(initConfig);
	}
	/**
	 *
	 * @param {*} params
	 * @returns
	 */
	createOrder(params) {
		let tradeType = '';
		// JSAPI 适用于：微信公众号支付、微信小程序支付、支付宝小程序支付
		// APP 适用于：支付宝、微信APP支付
		// NATIVE 适用于：支付宝、微信PC网站扫码支付
		// MWEB 适用于：微信h5（非公众号）跳转微信页面支付
		switch (this.method) {
			case 'mp':
			case 'miniapp':
				tradeType = 'JSAPI';
				break;
			case 'app':
				tradeType = 'APP';
				break;
			case 'scan':
				tradeType = 'NATIVE';
				break;
			case 'wap':
				tradeType = 'MWEB';
				break;
		}
		
		const payData = {
			tradeType,
			...params,
		};
		// 回调地址拼接
		if(!params.notifyUrl.startsWith('http') && this.notifyUrl){
			payData.notifyUrl = this.notifyUrl + params.notifyUrl;
		}
		if (this.paytype === 'wechat' && this.method === 'wap') {
			const h5_info = this.config.wap.sceneInfo.h5_info;
			if (this.version === 'v2') {
				payData.sceneInfo = {
					payer_client_ip: params.spbillCreateIp,
					h5_info: {
						...h5_info,
					},
				};
			} else {
				payData.sceneInfo = {
					payer_client_ip: params.spbillCreateIp,
					h5_info: {
						type: h5_info.type,
						app_name: h5_info.wap_name,
						app_url: h5_info.wap_url,
					},
				};
			}
			if (params.sceneInfo !== void 0) {
				payData.sceneInfo = {
					...payData.sceneInfo,
					...params.sceneInfo,
				};
			}
		}
		return this.unipayIns.getOrderInfo(payData);
	}
	queryOrder(params) {
		return this.unipayIns.queryOrder(params);
	}
	closeOrder(params) {
		return this.unipayIns.closeOrder(params);
	}
	cancelOrder(params) {
		return this.unipayIns.cancelOrder(params);
	}
	refundQuery(params) {
		return this.unipayIns.refundQuery(params);
	}
	async notify(event) {
		const verifyResult = await this.unipayIns.verifyPaymentNotify(event);
		if (!verifyResult) {
			console.log('---------!签名验证未通过!---------');
			return {};
		}
		return {
			...verifyResult,
			version: this.version,
		};
	}
}
module.exports = Pay;
